package com.bw.house.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupMenu
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.transition.Visibility
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmFragment
import com.bw.house.R
import com.bw.house.adapter.HouseAdapter
import com.bw.house.adapter.ItemAdapter
import com.bw.house.adapter.TabAdapter
import com.bw.house.databinding.FragmentHouseBinding
import com.bw.house.result.HouseResult
import com.bw.house.viewmodel.HouseViewModel


/**
 * 智慧园区-03-房源信息V1.1
 * 房源信息
 * 刘晓贺
 * 20250510
 */
class HouseFragment : BaseMvvmFragment<FragmentHouseBinding,HouseViewModel>() {


    private val tabAdapter:TabAdapter= TabAdapter()

    private val houseAdapter:HouseAdapter= HouseAdapter()

    private val itemAdapter:ItemAdapter= ItemAdapter(houseAdapter)

    override fun initView() {
        val data:MutableList<String> = mutableListOf()
        data.add("1栋")
        data.add("2栋")
        data.add("3栋")
        itemAdapter.data.clear()
        itemAdapter.data.addAll(data)

        vm.getHouse()
        binding.tab.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        binding.tab.adapter=this.tabAdapter

        binding.rv.adapter=itemAdapter
        binding.rv.layoutManager=LinearLayoutManager(context)

        binding.back.setOnClickListener {
            activity?.finish()
        }




        binding.more.setOnClickListener {
            PopupMenu(context,it).apply {

                menuInflater.inflate(R.menu.menu_system,menu)
                setOnMenuItemClickListener {
                    when(it.itemId){
                        R.id.UserManager->{
                            ARouter.getInstance().build(RoutePath.PATH_HOUSE_USER).navigation()
                            true
                        }
                        else -> {
                            false
                        }
                    }
                }
                show()
            }
        }
    }

    override fun initData() {
        vm.houseSuccess.observe(this){
            val data=it.groupBy {
                it.regionName
            }.map {
                it.value.first()
            }
            tabAdapter.data.clear()
            tabAdapter.data.addAll(data)
            tabAdapter.notifyDataSetChanged()
            houseAdapter.data.clear()
            houseAdapter.data.addAll(it)
            houseAdapter.notifyDataSetChanged()
        }
    }


}